# Bivariate plots ####

ven_mun  <- readRDS("ven_mun_elec_2006_2024_final.rds")


names(ven)


ven_mun %>% ggplot(., aes(x = log(pob_2011), y = of_p)) + geom_point() +
  facet_wrap(~year)

names(ven_mun)

## Ponpulation size ####

ven_mun %>%
  ggplot(aes(x = log(pob_2011), y = of_p*100, color = factor(year))) + 
  geom_point(alpha = 0.5) +
  geom_smooth(method = "loess", se = F) +
  labs(
    title = "Share of vote for Chavismo vs. population size",
    x = "Log(Population 2011)",
    y = "% Chavismo",
    color = "Election Year"
  ) +
  theme_minimal() + theme(axis.title.y = element_text(vjust = 2, size = 9),
                          title = element_text(size = 10))

av_vote_share_ch_pop <- ven_mun %>%
  group_by(mun_edo, pob_2011) %>% 
  summarise(p_chavismo = weighted.mean(of_p, validos, na.rm = TRUE)) %>% 
  ggplot(aes(x = log(pob_2011), y = p_chavismo)) + 
  geom_point(alpha = 0.5) +
  geom_smooth(method = "lm") +
  labs(
    title = "Average share of vote for Chavismo vs. population size",
    subtitle = "Venezuela Presidential elections 2006-2024",
    x = "Log(Population 2011)",
    y = "% Chavismo",
  ) +
  theme_minimal() + theme(axis.title.y = element_text(vjust = 2, size = 9),
                          title = element_text(size = 10))

av_vote_share_ch_pop



ven_mun %>%
  ggplot(aes(x = log(pob_2011), y = of_p*100)) + 
  geom_point(alpha = 0.5, color = "blue") +  # Transparency to reduce overplotting
  geom_smooth(method = "loess", color = "red") +  # Trend line
  facet_wrap(~year, scales = "fixed") +  # Keeps axes consistent
  labs(
    title = "Relationship Between Municipality Size and Government Vote Share",
    subtitle = "Venezuelan Presidential Elections",
    x = "Log(Population 2011)",
    y = "Government Vote Share (%)"
  ) +
  theme_minimal()


## Densidad ####

ven_mun %>%
  ggplot(aes(x = log(densidad_hab_km2), y = of_p, color = factor(year))) + 
  geom_point(alpha = 0.5) +
  geom_smooth(method = "loess") +
  labs(
    title = "Vote Share for Government vs. Municipality Size",
    subtitle = "Colored by Election Year",
    x = "Log(Population 2011)",
    y = "Government Vote Share (%)",
    color = "Election Year"
  ) +
  theme_minimal()

ven_mun %>%
  ggplot(aes(x = log(densidad_hab_km2), y = of_p*100)) + 
  geom_point(alpha = 0.5, color = "blue") +  # Transparency to reduce overplotting
  geom_smooth(method = "lm", color = "red") +  # Trend line
  facet_wrap(~year, scales = "fixed") +  # Keeps axes consistent
  labs(
    title = "Relationship Between Municipality Size and Government Vote Share",
    subtitle = "Venezuelan Presidential Elections",
    x = "Log(Densidad)",
    y = "Government Vote Share (%)"
  ) +
  theme_minimal()


## Population size categories ####

ven_mun %>%
  group_by(mun_size_hab) %>% 
  summarise(
    n = n(),
    vote_of = weighted.mean(of_p, validos, na.rm = T),
            se_vote_of = sd(of_p, na.rm = TRUE)/sqrt(n())) %>% 
  ggplot(aes(x = factor(mun_size_hab), y = vote_of)) + 
  geom_point() +
  geom_errorbar(aes(ymin = vote_of-1.96*se_vote_of,
                    ymax = vote_of+1.96*se_vote_of)) +
  labs(
    title = "Relationship Between Municipality Size and Chavismo Vote Share",
    subtitle = "Venezuelan Presidential Elections",
    x = "Municipality size",
    y = "Chavismo Vote Share (%)"
  ) +
  theme_minimal()

ven_mun %>%
  group_by(urb_level_onu) %>% 
  summarise(
    n = n(),
    vote_of = weighted.mean(of_p, validos, na.rm = TRUE),
    se_vote_of = (sd(of_p, na.rm = TRUE) / sqrt(n()))  # Convert SE to %
  ) %>% 
  drop_na(urb_level_onu) %>% 
  mutate(urb_level_onu = factor(urb_level_onu, levels = c("rural", "urban-town", "urban"), 
                                labels = c("Rural", "Intermediate", "Urban"))) %>% 
  ggplot(aes(x = factor(urb_level_onu), y = vote_of)) + 
  geom_point(size = 3) +
  geom_errorbar(aes(ymin = vote_of - 1.96 * se_vote_of,
                    ymax = vote_of + 1.96 * se_vote_of), width = 0.2) +
  labs(
    title = "Average share of vote by Urban-Rural municipality",
    subtitle = "Presidential Elections in Venezuela",
    x = "",
    y = "Chavismo Vote Share (%)"
  ) + theme_minimal()


ven_mun %>%
  group_by(urb_level_onu) %>% 
  summarise(
    n = n(),
    vote_of = weighted.mean(of_p, validos, na.rm = TRUE),
    vote_op = weighted.mean(op_p, validos, na.rm = TRUE),
    se_vote_of = sd(of_p, na.rm = TRUE) / sqrt(n()),
    se_vote_op = sd(op_p, na.rm = TRUE) / sqrt(n())
  ) %>% 
  drop_na(urb_level_onu) %>% 
  mutate(urb_level_onu = factor(urb_level_onu, levels = c("rural", "urban-town", "urban"), 
                                labels = c("Rural", "Intermediate", "Urban"))) %>%
  pivot_longer(cols = c(vote_of, vote_op), names_to = "Political Block", values_to = "Vote Share") %>%
  mutate(`Political Block` = recode(`Political Block`, "vote_of" = "Chavismo", "vote_op" = "Opposition")) %>%
  ggplot(aes(x = urb_level_onu, y = `Vote Share`, color = `Political Block`, group = `Political Block`)) + 
  geom_point(size = 3) +
  geom_errorbar(aes(ymin = `Vote Share` - 1.96 * ifelse(`Political Block` == "Chavismo", se_vote_of, se_vote_op),
                    ymax = `Vote Share` + 1.96 * ifelse(`Political Block` == "Chavismo", se_vote_of, se_vote_op)), 
                width = 0.2) +
  labs(
    title = "Average Vote Share by Urban-Rural Classification",
    subtitle = "Presidential Elections in Venezuela",
    x = "",
    y = "Vote Share (%)",
    color = "Political Block"
  ) + 
  theme_minimal()

ven_mun %>%
  mutate(urb_level_onu = factor(urb_level_onu, levels = c("rural", "urban-town", "urban"), 
                                labels = c("Rural", "Intermediate", "Urban"))) %>% 
  drop_na(urb_level_onu) %>% 
  ggplot(aes(x = factor(urb_level_onu), y = of_p)) + 
  geom_boxplot() +
  labs(
    title = "Average share of vote by Urban-Rural municipality",
    subtitle = "Presidential Elections in Venezuela",
    x = "",
    y = "Chavismo Vote Share (%)"
  ) + theme_minimal()


group_by(mun_edo, urb_level_onu) %>% 
  summarise(
    n = n(),
    vote_of = weighted.mean(of_p, validos, na.rm = TRUE)
  ) %>% 
  drop_na(urb_level_onu) %>% 